package com.fg.cloud.manager.service.agency.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.construction.Select;
import com.fg.cloud.common.encrypt.Encrypt;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.agency.AgencyAccountEntity;
import com.fg.cloud.manager.entry.agency.AgencyAddressEntity;
import com.fg.cloud.manager.entry.agency.AgencyEntity;
import com.fg.cloud.manager.entry.agency.AgencyImagesEntity;
import com.fg.cloud.manager.entry.agency.AgencyUserEntity;
import com.fg.cloud.manager.entry.agency.vo.AgencyInvoiceVo;
import com.fg.cloud.manager.entry.agency.vo.AgencyVo;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.agency.AgencyAccountMapper;
import com.fg.cloud.manager.mapper.agency.AgencyAddressMapper;
import com.fg.cloud.manager.mapper.agency.AgencyMapper;
import com.fg.cloud.manager.mapper.agency.AgencyUserMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.agency.AgencyService;

/**
 * 经销商信息service实现
 * @author around
 * @date 2017-6-21
 */
@Service
public class AgencyServiceImpl extends BaseServiceImpl implements AgencyService {

	@Autowired
	private AgencyMapper agencyMapper;
	@Autowired
	private AgencyAccountMapper agencyAccountMapper;
	@Autowired
	private AgencyUserMapper agencyUserMapper;
	@Autowired
	private AgencyAddressMapper agencyAddressMapper;

	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return agencyMapper;
	}
	
	@Override
	@Transactional
	public boolean deletes(String key, String values) throws IOException {
		return agencyMapper.deletes(key, values.split("[, ]+")) > 0 ? true : false;
	}

	@Override
	public void findList(PageUtils<AgencyVo> pageinfo) {
		long total = agencyMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if(total > 0) {
			List<AgencyVo> agencyList = agencyMapper.findList(pageinfo);

			for (AgencyVo agency : agencyList) {
				agency.setImgList(agencyMapper.selectImage(agency.getId()));
			}
			pageinfo.setData(agencyList);
		} else {
			pageinfo.setData(new ArrayList<AgencyVo>());
		}
	}

	@Override
	@Transactional
	public boolean deletes(Long userId, Long facId, Long[] ids) {
		agencyMapper.deleteImgs(ids);
		return agencyMapper.updateStatus(userId, facId, ids, -1) > 0 ? true : false;
	}

	@Override
	public List<Select> findBySelect(AgencyVo agency, String filter) {
		return agencyMapper.selectBySelect(agency, filter);
	}
	
	@Override
	public List<Select> selectNewsBySelect(Long facId, String filter) {
		return agencyMapper.selectNewsBySelect(facId, filter);
	}

	@Override
	@Transactional
	public boolean insertImgs(String imgs,Long agencyId) {
		Long[] ids= new Long[]{agencyId}; 
		agencyMapper.deleteImgs(ids);

		return agencyMapper.insertImgs(agencyId, imgs.split("[, ]+")) > 0 ? true : false;
	}

	@Override
	public List<AgencyImagesEntity> findImgs(Long agencyId) {
		return agencyMapper.selectImgs(agencyId);
	}

	@Override
	public AgencyVo find(Long id) {
		AgencyVo agency = agencyMapper.selectByPrimaryKey(id);
		agency.setImgList(agencyMapper.selectImage(id));
		return agency;
	}

	@Override
	public AgencyInvoiceVo finds(Long id) {
		
		return agencyMapper.finds(id);
	}

	@Override
	public boolean selectBycondition(Long id, Long facId) {
		return agencyMapper.selectBycondition(id, facId)>0 ? true : false;
	}

	@Override
	@Transactional
	public boolean deleteImgs(Long ageId, String picUrl) {
		return agencyMapper.deletePic(ageId,picUrl) > 0 ? true : false ;
	}

	@Override
	@Transactional
	public boolean addImgs(AgencyImagesEntity images) {
		return agencyMapper.addImgs(images) > 0 ? true : false ;
	}
	
	@Override
	public List<AgencyVo> findListByFactory(Long facId) {
		
		return agencyMapper.selectByFactory(facId);
	}

	@Override
	public List<Select> findListByFacId(Long facId) {
		return agencyMapper.findListByFacId(facId);
	}
	
	

	@Override
	@Transactional
	public boolean insertSelective(AgencyVo entity) {
		//新增经销商信息
		boolean flag = agencyMapper.insertSelective(entity) > 0;
		//新增经销商图册
		if (null != entity.getImgList() && entity.getImgList().size() > 0) {
			agencyMapper.insertImgs(entity.getId(), entity.getImgList()
					.toArray(new String[entity.getImgList().size()]));
		}
		//新增经销商账户
		AgencyAccountEntity accountCash=new AgencyAccountEntity();
		accountCash.setAgeId(entity.getId());
		accountCash.setFacId(entity.getFacId());
		accountCash.setCreator(entity.getCreator());
		accountCash.setUpdater(entity.getCreator());
		agencyAccountMapper.insertSelective(accountCash);
		return flag;
	}
	
	/**
	 * 导入经销商并初始化经销商相关数据
	 */
	@Override
	@Transactional
	public boolean importAgency(AgencyEntity entity) {
		//新增经销商
		boolean flag1 = agencyMapper.insertSelective(entity) > 0;
		//初始化经销商账户
		AgencyAccountEntity accountCash = new AgencyAccountEntity();
		accountCash.setAgeId(entity.getId());
		accountCash.setFacId(entity.getFacId());
		accountCash.setCreator(entity.getCreator());
		accountCash.setUpdater(entity.getCreator());
		boolean flag2 = agencyAccountMapper.insertSelective(accountCash) > 0;
		//初始化经销商登录用户
		AgencyUserEntity user = new AgencyUserEntity();
		user.setUserName(entity.getAgeName());
		user.setPassword(Encrypt.md5("123456"));
		user.setStatus(1);
		user.setFacId(entity.getFacId());
		user.setAgeId(entity.getId());
		user.setIsShowPrice(1);
		user.setCreator(entity.getCreator());
		user.setUpdater(entity.getCreator());
		boolean flag3 = agencyUserMapper.insertSelective(user) > 0;
		//默认收货地址
		AgencyAddressEntity address = new AgencyAddressEntity();
		address.setAgeId(entity.getId());
		address.setStatus(1);
		address.setCusName(entity.getAgeName());
		address.setReceiver(entity.getContact());
		address.setProvince(entity.getProvince());
		address.setCity(entity.getCity());
		address.setArea(entity.getArea());
		address.setAddress(entity.getAddress());
		address.setZipcode(entity.getZipcode());
		if (entity.getPhone() != null)
			address.setPhone(entity.getPhone());
		if (entity.getTel() != null) 
			address.setTel(entity.getTel());
		address.setIsDefault(1);
		address.setCreator(entity.getCreator());
		address.setUpdater(entity.getCreator());
		boolean flag4 = agencyAddressMapper.insertSelective(address) > 0;
		
		if ( flag1 && flag2 && flag3 && flag4 ) {
			return true;
		}

		return false;
	}
	
	@Override
	@Transactional
	public  boolean updateByPrimaryKeySelective(AgencyVo entity) {
		boolean flag = agencyMapper.updateByPrimaryKeySelective(entity) > 0 ? true : false;
		//删除旧图
		agencyMapper.deleteImgs(new Long[]{entity.getId()});
		
		if (null != entity.getImgList() && entity.getImgList().size() > 0)
			agencyMapper.insertImgs(entity.getId(), entity.getImgList()
					.toArray(new String[entity.getImgList().size()]));
		
		/* 方案2：使用2边的各自处理模式
		//旧图片
		List<String> oldImgList = agencyMapper.selectImage(entity.getId());
		//等待插入的图片
		List<String> addList = DefaultListUtils.equals(null == entity.getImgList() ?
				new ArrayList<String>() : entity.getImgList(), oldImgList);
		//等待删除的图片
		List<String> removeList = DefaultListUtils.equals(oldImgList, entity.getImgList());
		
		if (null != addList && addList.size() > 0)
			agencyMapper.insertImgs(entity.getId(), addList.toArray(new String[addList.size()]));
		
		if (null != removeList && removeList.size() > 0)
			agencyMapper.batchDeleteImage(entity.getId(), removeList);*/
		
		return flag;
	}

	@Override
	public boolean verifyUniquely(Long id, Long facId, String field, Object value) {
		return agencyMapper.verifyUniquely(id, facId, field, value) > 0 ? false : true;
	}

	

}
